<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button @click="export_detail">导出明细</el-button>
            <el-button @click="export_all">导出汇总</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="配送日期" prop="send_time">
              <el-date-picker
                v-model="form.send_time"
                type="date"
                class="wid220"
                :editable="false"
                placeholder="选择配送日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
              />
            </el-form-item>
            <el-form-item label="商品分类" prop="category_id" class="wid220">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
              ></el-cascader>
            </el-form-item>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-if="moredata">
                <!-- <el-form-item label="仓库" prop="name1">
                  <el-input
                    v-model.trim="form.name1"
                    placeholder="仓库"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item> -->
                <el-form-item label="商品名称" prop="goods_spu_id">
                  <change-goods-input
                    ref="changeGoodsInput"
                    @clear="
                      () => {
                        this.form.goods_spu_id = '';
                      }
                    "
                    @sendValue="
                      (data) => {
                        this.form.goods_spu_id = data.id;
                      }
                    "
                  ></change-goods-input>
                  <!-- <el-input
                    v-model.trim="form.name"
                    placeholder="商品名称"
                    clearable
                    class="wid220"
                  ></el-input> -->
                </el-form-item>
                <el-form-item label="配送时间" prop="send_time_id">
                  <el-select
                    v-model.trim="form.send_time_id"
                    placeholder="配送时间"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(send_time_list, index) in list.send_time_list"
                      :key="index"
                      :label="send_time_list"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="配送线路" prop="line_id">
                  <el-select
                    v-model.trim="form.line_id"
                    placeholder="配送线路"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(line, index) in list.line"
                      :key="index"
                      :label="line"
                      :value="index"
                    ></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="用户分组" prop="group_id">
                  <el-select
                    v-model.trim="form.group_id"
                    placeholder="用户分组"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(group, index) in list.group"
                      :key="index"
                      :label="group"
                      :value="index"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="用户区域" prop="region_id">
                  <el-select
                    v-model.trim="form.region_id"
                    placeholder="用户区域"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(region, index) in list.region"
                      :key="index"
                      :label="region"
                      :value="index"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <div class="clearfix"></div>
              </div>
            </el-collapse-transition>
            <el-form-item class="marginTop10">
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 750px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :row-class-name="getRowClass"
            @expand-change="expandChange"
          >
            <!-- @select="onTableSelect" -->
            <el-table-column type="expand" class-name="main_hei">
              <template slot-scope="props">
                <el-table
                  :ref="'expandchild' + props.$index"
                  :data="props.row.order_list"
                  class="mainex_ta"
                  fit
                  :row-class-name="getRowClass"
                  :highlight-current-row="false"
                >
                  <!-- @select="
                    (rows, row) => {
                      expandSelect(rows, row, props.$index);
                    }
                  " -->
                  <!-- <el-table-column type="selection" width="95" align="right" /> -->
                  <el-table-column width="40" align="right" />
                  <el-table-column
                    label="订单号"
                    :show-overflow-tooltip="true"
                    prop="order_sn"
                    min-width="110"
                  />
                  <el-table-column
                    label="用户名称"
                    prop="nickname"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="下单数量"
                    prop="order_num"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="下单单价"
                    prop="order_price"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="下单金额"
                    prop="order_money"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="配送数量"
                    prop="sort_num"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="配送单价"
                    prop="sort_price"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="配送金额"
                    prop="send_money"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="退货数量"
                    prop="return_num"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="退货金额"
                    prop="return_money"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="实配送金额"
                    prop="really_money"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="备注"
                    prop="remark"
                    show-overflow-tooltip
                  />
                </el-table>
              </template>
            </el-table-column>

            <!-- <el-table-column type="selection" /> -->
            <el-table-column label="序号">
              <template slot-scope="scope">{{
                (current.page - 1) * current.per_page + scope.$index + 1
              }}</template>
            </el-table-column>
            <el-table-column
              label="商品名称"
              prop="name"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column label="单位" prop="unit" show-overflow-tooltip />

            <el-table-column
              label="分类"
              prop="category_name"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              label="市场价"
              prop="market_price"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              label="库存"
              prop="inventory_num"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              label="下单总量"
              prop="order_num"
              show-overflow-tooltip
            />
            <el-table-column
              label="下单总额"
              prop="order_money"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                {{ parseFloat(scope.row.order_money) | twoPlace }}
              </template>
            </el-table-column>
            <el-table-column
              label="配送总量"
              prop="send_num"
              show-overflow-tooltip
            />
            <el-table-column
              label="配送总额"
              prop="send_money"
              show-overflow-tooltip
            />
            <el-table-column
              label="退货总量"
              prop="return_num"
              show-overflow-tooltip
            />
            <el-table-column
              label="退货总额"
              prop="return_money"
              show-overflow-tooltip
            />
            <el-table-column
              label="实配送总额"
              prop="really_money"
              show-overflow-tooltip
            />
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  orderList_index,
  orderSummary_export_detail,
  orderSummary_export_all,
} from "@/api/order/orderSummary";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category } from "@/utils/index";
import { parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
        region: {},
        group: {},
      },
      listLoading: false,
      // 搜索
      form: {
        name1: "",
        goods_spu_id: "",
        pid: "",
        category_id: [],
        send_time: parseTime(new Date(), "{y}-{m}-{d}"),
        send_time_id: null,
        line_id: null,
        group_id: null,
        region_id: null,
      },
      // 更多
      moredata: false,
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    this.orderList_index();
  },
  methods: {
    // 时间

    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      row.row.index = row.rowIndex;
      if (row.row.order_list) {
        if (row.row.order_list.length === 0) {
          return "row-expand-cover";
        }
      }
    },
    // 监听展开行
    expandChange(rows, row) {
      // console.log(rows)
      // console.log(row)
      if (rows.checked) {
        rows.order_list.forEach((chil) => {
          setTimeout(() => {
            if (chil.checked) {
              if (this.$refs["expandchild" + rows.index]) {
                this.$refs["expandchild" + rows.index].toggleRowSelection(
                  chil,
                  true
                );
              }
            }
          });
        });
      }
    },
    // 监听一级是否勾选
    onTableSelect(rows, row) {
      // console.log(rows, row)
      row.checked = !row.checked;
      if (row.checked) {
        row.order_list.forEach((m) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].toggleRowSelection(m, true);
          }
          m.checked = true;
        });
      } else {
        row.order_list.forEach((i) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].clearSelection();
          }
          i.checked = false;
        });
      }
    },
    // 监听二级是否勾选
    expandSelect(rows, row, index) {
      // console.log(rows)
      // console.log(row)
      // console.log(index)
      row.checked = !row.checked;
      if (rows.length != 0) {
        this.list.list[index].checked = true;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          true
        );
      } else {
        this.list.list[index].checked = false;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          false
        );
      }
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.orderList_index();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      if (this.$refs["changeGoodsInput"]) {
        this.$refs["changeGoodsInput"].name = "";
      }
      this.getArticleList();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.orderList_index();
    },
    changePage(page) {
      this.current.page = page;
      this.orderList_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    orderList_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current, {
                status: this.activeName,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current,
        { status: this.activeName }
      );
      orderList_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          res.data.list.map((e) => {
            e.checked = false;
            e.order_list.map((p) => {
              p.checked = false;
            });
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);

      return form;
    },
    // 导出明细
    export_detail() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      orderSummary_export_detail(data)
        .then((res) => {
          const link = document.createElement("a");
          // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let blob = new Blob([res], {
            type:
              "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "订单明细";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 导出汇总
    export_all() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      orderSummary_export_all(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "订单汇总";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }
  .mainor_ter {
    margin-top: 4px;
    /deep/.el-button {
      margin-left: 5px;
    }
    a {
      white-space: nowrap;
      border: 1px solid $menuActiveText;
      background-color: $menuActiveText;
      padding: 7px 10px;
      color: $white;
      -webkit-appearance: none;
      text-align: center;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      -webkit-transition: 0.1s;
      transition: 0.1s;
      font-weight: 500;
      font-size: $font_14;
      border-radius: 4px;
    }
  }
  .main_table {
    margin-top: 20px;
  }
}
</style>
